
(function(context){
	const { ElMessage, ElMessageBox } = ElementPlus;
	//站点路径
	const baseUrl = "/"
	// 接口地址
	const apiBaseURL = "/api"
	// axios实例
	context.service = axios.create({
		baseURL: apiBaseURL,
		timeout: 60000,
		headers: {
			'Content-Type': 'application/json;charset=UTF-8'
		}
	})

	// 请求拦截器
	service.interceptors.request.use(
		(config) => {
			config.headers['X-Requested-With'] = 'XMLHttpRequest'
			return config
		},
		error => {
			return Promise.reject(error)
		}
	)

	service.interceptors.response.use(
		async (response) => {

			// if (response.data.status !== 200) {
			// 	return Promise.reject(new Error(response.statusText || 'Error'))
			// } 
			if (response.data.code !== 200) {
				// 响应失败
				ElementPlus.ElMessage.error(response.data.msg)
				return Promise.reject(new Error(response.statusText || 'Error'))
			}

			// 响应成功
			return  Promise.resolve(response.data)
		},
		error => {
			return Promise.reject(error)
		}
	)

	context.Api = {
		// 添加用户
		addUser: (params)=>service.post("/user/add",params),
		// 修改用户
		editUser: (params)=>service.post("/user/edit",params),
		// 删除用户
		delUser: (id)=>service.post("/user/del",{"id": id}),
		// 查询用户
		getUserList: (params)=>service.get("/user/page",{params:params}),
		// 重置用户密码
		resetUserPwd: (id)=>service.post("/user/resetpwd?id="+id),
		// 重置自己密码
		updatePwd: (params)=>service.post("/user/updatepwd", params),
		// 重置密码
		resetUserUnlock: (id)=>service.post("/user/unlock?id="+id),
		// 批量导入用户
		importUserApiUrl: (formData)=>service.post("/user/import", formData, {
			headers: {
				'Content-Type': 'multipart/form-data'
			}
		}),
	}
	

})(window);
